#include <bits/stdc++.h>
using namespace std;
template <uint32_t mod> class mod_int{
  private:
    static constexpr uint32_t get_r(){
      uint32_t ret = mod;
      for(int i = 0; i < 4; i++) ret *= 2 - mod * ret;
      return ret;
    }
    static constexpr uint32_t r = get_r();
    static constexpr uint32_t n2 = -uint64_t(mod) % mod;
    static_assert(r * mod == 1 && mod < (1 << 30) && mod & 1);
    uint32_t data;
  public:
    constexpr mod_int(): data(0){}
    template <class int_t> constexpr mod_int(const int_t x): data(reduce(uint64_t((sizeof(int_t) < sizeof(uint32_t)? x: x % int_t(mod)) + mod) * n2)){};
    static constexpr uint32_t reduce(const uint64_t x){return (x + uint64_t(uint32_t(x) * (-r)) * mod) >> 32;}
    constexpr mod_int& operator += (const mod_int &r){if(int32_t(data += r.data - 2 * mod) < 0){data += 2 * mod;} return *this;}
    constexpr mod_int& operator -= (const mod_int &r){if(int32_t(data -= r.data) < 0){data += 2 * mod;} return *this;}
    constexpr mod_int& operator *= (const mod_int &r){return data = reduce((uint64_t)data * r.data), *this;}
    constexpr mod_int& operator /= (const mod_int &r){return *this *= r.inv();}
    constexpr friend mod_int operator + (mod_int l, const mod_int &r){return l += r;}
    constexpr friend mod_int operator - (mod_int l, const mod_int &r){return l -= r;}
    constexpr friend mod_int operator * (mod_int l, const mod_int &r){return l *= r;}
    constexpr friend mod_int operator / (mod_int l, const mod_int &r){return l /= r;}
    constexpr mod_int operator - () const {return mod_int() - mod_int(*this);}
    template <class int_t> constexpr mod_int pow(int_t r) const {mod_int res(1), w(*this); for(; r; r >>= 1, w *= w) if(r & 1) res *= w; return res;}
    constexpr mod_int inv() const {return pow(mod - 2);}
    constexpr uint32_t value() const {uint32_t res = reduce(data); return res >= mod? res - mod: res;}
}; typedef mod_int<998244353> mint;
constexpr int MAXN = 10000005;
int _, n, A, B, C, M, V[MAXN];
int main(){
  freopen("guess.in", "r", stdin);
  freopen("guess.out", "w", stdout);
  cin.tie(nullptr)->sync_with_stdio(false);
  for(cin >> _; _--;){
    cin >> n >> V[0] >> A >> B >> C >> M; mint Ans = 0;
    for(int i = 1; i <= n; i++) V[i] = (((int64_t)A * V[i - 1] + B) % M * V[i - 1] + C) % M + 1;
    sort(V + 1, V + n + 1); int tot = 0;
    for(int l = 1, r; l <= n; l = r){
      for(r = l; r <= n && V[l] == V[r]; r++);
      tot += (l > 1) + (r <= n) + (r - l > 1);
    }
    Ans = 1 - mint(tot).inv() * 2;
    cout << 0.1 << ' ' << Ans.value() << '\n';
  }
  return 0;
}
